package com.yzx.lifeassistants.model.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.yzx.lifeassistants.bean.ActivityInfo;
import com.yzx.lifeassistants.model.IQueryActivity;
import com.yzx.lifeassistants.model.callback.IQueryActivityCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.PropertyUtil;

/**
 * @Description: 查询活动预告
 * @author: yzx
 * @time: 2016-3-17 下午8:57:18
 */
public class QueryActivityImpl implements IQueryActivity {
	public static final String TAG = "QueryActivityImpl";
	private String refererUrl;// URL
	private IQueryActivityCB queryCallBack;// 回调

	public QueryActivityImpl(Context context, IQueryActivityCB queryCallBack) {
		this.queryCallBack = queryCallBack;
		Properties properties = PropertyUtil.getProperties(context,
				"base.properties");
		refererUrl = properties.getProperty("activity_referer_url");
	}

	/**
	 * @Description: 查询活动预告
	 */
	@Override
	public void queryActivity(int page) {
		String url = this.refererUrl + "/html/xjzxwz/" + page + ".html";
		HttpClientUtil.sendRequestWithGBK(url, null, new IHttpCallBack() {

			@Override
			public void onFinish(String response) {
				LogcatUtils.i(response);
				if (null == response || "".equals(response)) {
					LogcatUtils.e("获取数据失败");
					queryCallBack.queryError(IQueryActivityCB.RESULR_NULL);
				} else {
					List<ActivityInfo> activityInfos = new ArrayList<ActivityInfo>();
					Document doc = Jsoup.parse(response);
					Element divElement = doc.select("div.list_main_content>ul")
							.first();
					Elements elements = divElement.select("li");
					for (Element element : elements) {
						ActivityInfo activityInfo = new ActivityInfo();
						String time = element.select("span").first().text();
						LogcatUtils.i(">>>>>>>>>>>" + time);
						if (haveData(time)) {
							activityInfo.setTime(time);
						}
						String url = element.select("a").first().attr("href");
						LogcatUtils.i(">>>>>>>>>>>>" + url);
						if (haveData(url)) {
							activityInfo.setUrl(refererUrl + url);
						}
						String title = element.select("a").first().text();
						LogcatUtils.i(">>>>>>>>>>>>" + title);
						if (haveData(title)) {
							activityInfo.setTitle(title);
						}
						if (haveData(activityInfo.getTime())
								&& haveData(activityInfo.getUrl())
								&& haveData(activityInfo.getTitle())) {
							activityInfos.add(activityInfo);
						}
					}
					if (null == activityInfos || 1 > activityInfos.size()) {
						LogcatUtils.e("获取数据失败");
						queryCallBack.queryError(IQueryActivityCB.RESULR_NULL);
					} else {
						LogcatUtils.i("获取数据成功");
						queryCallBack.querySuccess(activityInfos);
					}
				}
			}

			@Override
			public void onError(Exception e) {
				if (null != e.getMessage()) {
					LogcatUtils.e(TAG, e.getMessage());
				}
				queryCallBack.queryError(IQueryActivityCB.QUERY_ERROR);
			}
		});
	}

	/**
	 * 
	 * @Description: 是否有数据
	 */
	private Boolean haveData(String data) {
		if (null == data || "".equals(data)) {
			return false;
		} else {
			return true;
		}
	}
}
